package com.service.calculate.vehicle;

import com.service.bean.device.Vehicle;
import com.service.bean.oil.OilBoxStandard;
import com.service.other.Logger;

public class CalculatorOil {
	
	/**
	 * 计算实际油量
	 * @param vehicle 车辆
	 * @param fuelHeight 当前油量(升)
	 * @return
	 */
	public static double getOilValue(Vehicle vehicle, double fuelHeight) {
		OilBoxStandard first = null;
		OilBoxStandard second = null;
		for (int i=1; i<vehicle.oilBox.standards.size(); i++) {
			first = vehicle.oilBox.standards.get(i-1);
			second = vehicle.oilBox.standards.get(i);
			if (fuelHeight==first.oilHeight) {
				return first.oilValue;
			} else if (fuelHeight == second.oilHeight) {
				return second.oilValue;
			} else if (fuelHeight > first.oilHeight && fuelHeight < second.oilHeight) {
				return cal(first, second, fuelHeight);
			} else if (i==(vehicle.oilBox.standards.size()-1) && fuelHeight > second.oilHeight) {
				// 超出最大值
				return cal(first, second, fuelHeight);
			}
		}
		return 0;
	}
	
	private static double cal(OilBoxStandard first, OilBoxStandard second, double fuelHeight) {
		if ((second.oilValue - first.oilValue) == 0) {
			Logger.err("OilBoxStandard中，上下两条数据的oilValue相同" + first.oilBoxId);
			return 0;
		}
		double x = (second.oilHeight - first.oilHeight) / (second.oilValue - first.oilValue);
		double y = (fuelHeight - second.oilHeight) / x + second.oilValue;
		return y;
	}
	
}
